﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JY.Common.Extensions;
using JY.Wpf.Controls.Converters;

namespace JY.Wpf.Controls.Controls
{
    /// <summary>
    /// Balance.xaml 的交互逻辑
    /// </summary>
    public partial class Balance : UserControl
    {
        /// <summary>
        /// 正常时的颜色
        /// </summary>
        protected static SolidColorBrush NormalColor;
        /// <summary>
        /// 接近时的颜色
        /// </summary>
        protected static SolidColorBrush NearColor;
        /// <summary>
        /// 符合时的颜色
        /// </summary>
        protected static SolidColorBrush AccordColor;
        /// <summary>
        /// 超出时的颜色
        /// </summary>
        protected static SolidColorBrush WarningColor;
        /// <summary>
        /// 构造器
        /// </summary>
        public Balance()
        {
            InitializeComponent();
            Balance.NormalColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF08FCF8"));
            Balance.NearColor = new SolidColorBrush(Colors.Yellow); ;
            Balance.AccordColor = new SolidColorBrush(Colors.GreenYellow);
            Balance.WarningColor = new SolidColorBrush(Colors.Red);
            this.Foreground = Balance.NormalColor;
        }

        #region # 依赖属性

        #region 百分数——double Percentage
        /// <summary>
        /// 百分数
        /// </summary>
        public double Percentage
        {
            get { return (double)GetValue(PercentageProperty); }
            set { SetValue(PercentageProperty, value); }
        }
        /// <summary>
        /// 百分数
        /// </summary>
        public static readonly DependencyProperty PercentageProperty = DependencyProperty.Register("Percentage", typeof(double), typeof(Balance), new PropertyMetadata(0.0, new PropertyChangedCallback(OnPercentagePropertyChanged)));

        private static void OnPercentagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion

        #region 允许起始值——double OptimalRangeStartValue
        /// <summary>
        /// 允许起始值
        /// </summary>
        public double OptimalRangeStartValue
        {
            get { return (double)GetValue(OptimalRangeStartValueProperty); }
            set { SetValue(OptimalRangeStartValueProperty, value); }
        }
        /// <summary>
        /// 允许起始值
        /// </summary>
        public static readonly DependencyProperty OptimalRangeStartValueProperty = DependencyProperty.Register("OptimalRangeStartValue", typeof(double), typeof(Balance), new PropertyMetadata(0.0));
        #endregion
        #region 允许起始值宽度——GridLength StartValueWidth
        /// <summary>
        /// 允许起始值宽度
        /// </summary>
        public GridLength StartValueWidth
        {
            get { return (GridLength)GetValue(StartValueWidthProperty); }
            set { SetValue(StartValueWidthProperty, value); }
        }
        /// <summary>
        /// 允许起始值宽度
        /// </summary>
        public static readonly DependencyProperty StartValueWidthProperty = DependencyProperty.Register("StartValueWidth", typeof(GridLength), typeof(Balance), new PropertyMetadata(new GridLength(50, GridUnitType.Star)));
        #endregion
        #region 接近允许起始值宽度——GridLength StartValueWidth
        /// <summary>
        /// 接近允许起始值宽度
        /// </summary>
        public GridLength NearStartValueWidth
        {
            get { return (GridLength)GetValue(NearStartValueWidthProperty); }
            set { SetValue(NearStartValueWidthProperty, value); }
        }
        /// <summary>
        /// 允许起始值宽度
        /// </summary>
        public static readonly DependencyProperty NearStartValueWidthProperty = DependencyProperty.Register("NearStartValueWidth", typeof(GridLength), typeof(Balance), new PropertyMetadata(new GridLength(30, GridUnitType.Star)));
        #endregion
        #region 最小接近允许起始值宽度——GridLength MinNearStartValueWidth
        /// <summary>
        /// 最小接近允许起始值宽度
        /// </summary>
        public GridLength MinNearStartValueWidth
        {
            get { return (GridLength)GetValue(MinNearStartValueWidthProperty); }
            set { SetValue(MinNearStartValueWidthProperty, value); }
        }
        /// <summary>
        /// 允许起始值宽度
        /// </summary>
        public static readonly DependencyProperty MinNearStartValueWidthProperty = DependencyProperty.Register("MinNearStartValueWidth", typeof(GridLength), typeof(Balance), new PropertyMetadata(new GridLength(20, GridUnitType.Star)));
        #endregion

        #region 保留小数位数——int KeepDigits
        /// <summary>
        /// 保留小数位数
        /// </summary>
        public int KeepDigits
        {
            get { return (int)GetValue(KeepDigitsProperty); }
            set { SetValue(KeepDigitsProperty, value); }
        }
        /// <summary>
        /// 允许结束值
        /// </summary>
        public static readonly DependencyProperty KeepDigitsProperty = DependencyProperty.Register("KeepDigits", typeof(int), typeof(Balance), new PropertyMetadata(3, new PropertyChangedCallback(OnKeepDigitsPropertyChanged)));

        private static void OnKeepDigitsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null) return;
        }

        #endregion

        #region 允许结束值——double OptimalRangeEndValue
        /// <summary>
        /// 允许结束值
        /// </summary>
        public double OptimalRangeEndValue
        {
            get { return (double)GetValue(OptimalRangeEndValueProperty); }
            set { SetValue(OptimalRangeEndValueProperty, value); }
        }
        /// <summary>
        /// 允许结束值
        /// </summary>
        public static readonly DependencyProperty OptimalRangeEndValueProperty = DependencyProperty.Register("OptimalRangeEndValue", typeof(double), typeof(Balance), new PropertyMetadata(0.0));
        #endregion

        #region 允许结束值宽度——GridLength EndValueWidth
        /// <summary>
        /// 允许结束值宽度
        /// </summary>
        public GridLength EndValueWidth
        {
            get { return (GridLength)GetValue(EndValueWidthProperty); }
            set { SetValue(EndValueWidthProperty, value); }
        }
        /// <summary>
        /// 允许结束值宽度
        /// </summary>
        public static readonly DependencyProperty EndValueWidthProperty = DependencyProperty.Register("EndValueWidth", typeof(GridLength), typeof(Balance), new PropertyMetadata(new GridLength(50,GridUnitType.Star)));
        #endregion

        #region 接近允许结束值宽度——GridLength NearEndValueWidth
        /// <summary>
        /// 接近允许结束值宽度
        /// </summary>
        public GridLength NearEndValueWidth
        {
            get { return (GridLength)GetValue(NearEndValueWidthProperty); }
            set { SetValue(NearEndValueWidthProperty, value); }
        }
        /// <summary>
        /// 接近允许结束值宽度
        /// </summary>
        public static readonly DependencyProperty NearEndValueWidthProperty = DependencyProperty.Register("NearEndValueWidth", typeof(GridLength), typeof(Balance), new PropertyMetadata(new GridLength(30, GridUnitType.Star)));
        #endregion

        #region 最大接近允许结束值宽度——GridLength MaxNearEndValueWidth
        /// <summary>
        /// 最大接近允许结束值宽度
        /// </summary>
        public GridLength MaxNearEndValueWidth
        {
            get { return (GridLength)GetValue(MaxNearEndValueWidthProperty); }
            set { SetValue(MaxNearEndValueWidthProperty, value); }
        }
        /// <summary>
        /// 最大接近允许结束值宽度
        /// </summary>
        public static readonly DependencyProperty MaxNearEndValueWidthProperty = DependencyProperty.Register("MaxNearEndValueWidth", typeof(GridLength), typeof(Balance), new PropertyMetadata(new GridLength(20, GridUnitType.Star)));
        #endregion

        #region 超出目标值显示——Visibility ExceedVisibility
        /// <summary>
        /// 超出目标值显示
        /// </summary>
        public Visibility ExceedVisibility
        {
            get { return (Visibility)GetValue(ExceedVisibilityProperty); }
            set { SetValue(ExceedVisibilityProperty, value); }
        }
        /// <summary>
        /// 超出目标值显示
        /// </summary>
        public static readonly DependencyProperty ExceedVisibilityProperty = DependencyProperty.Register("ExceedVisibility", typeof(Visibility), typeof(Balance), new PropertyMetadata(Visibility.Collapsed));
        #endregion
        #region 当前值——double CurrentValue
        /// <summary>
        /// 当前值
        /// </summary>
        public double CurrentValue
        {
            get { return (double)GetValue(CurrentValueProperty); }
            set { SetValue(CurrentValueProperty, value); }
        }
        /// <summary>
        /// 当前值
        /// </summary>
        public static readonly DependencyProperty CurrentValueProperty = DependencyProperty.Register("CurrentValue", typeof(double), typeof(Balance), new PropertyMetadata(0.0, new PropertyChangedCallback(OnCurrentValuePropertyChanged)));

        private static void OnCurrentValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Balance balance = d as Balance;
            if (balance == null) return;
            if(balance.TargetValue==0)return;
            //百分比
            balance.Percentage = balance.TargetValue==0?0:(balance.CurrentValue / balance.TargetValue) * 100;
            balance.ResidualValue = Math.Abs(balance.TargetValue == 0 ? 0 : balance.TargetValue - balance.CurrentValue);
            if (balance.CurrentValue.KeepDigits(balance.KeepDigits) > balance.TargetValue.KeepDigits(balance.KeepDigits))
            {
                balance.ExceedVisibility = Visibility.Visible;
            }
            else
            {
                balance.ExceedVisibility = Visibility.Collapsed;
            }
            #region # 设置接近目标值最小值最大值
            double nearStartValue = (balance.TargetValue - ((balance.TargetValue) * 0.2)).KeepDigits(balance.KeepDigits);
            double nearEndValue = (balance.TargetValue + ((balance.TargetValue) * 0.2)).KeepDigits(balance.KeepDigits);
            double minValue = (balance.TargetValue - ((balance.TargetValue) * 0.3)).KeepDigits(balance.KeepDigits);
            double maxValue = (balance.TargetValue + (balance.TargetValue - minValue)).KeepDigits(balance.KeepDigits);

            if (nearStartValue >= balance.OptimalRangeStartValue.KeepDigits(balance.KeepDigits))
            {

                double percentageSE = 0.001;

                if ((balance.TargetValue - balance.OptimalRangeStartValue).KeepDigits(balance.KeepDigits) >
                    (balance.OptimalRangeEndValue - balance.TargetValue).KeepDigits(balance.KeepDigits))
                {
                    percentageSE = (balance.TargetValue - balance.OptimalRangeStartValue) / balance.TargetValue;
                }
                else if ((balance.TargetValue - balance.OptimalRangeStartValue).KeepDigits(balance.KeepDigits) <
                    (balance.OptimalRangeEndValue - balance.TargetValue).KeepDigits(balance.KeepDigits))
                {
                    percentageSE = (balance.OptimalRangeEndValue - balance.TargetValue) / balance.TargetValue;
                }
                else if ((balance.TargetValue - balance.OptimalRangeStartValue).KeepDigits(balance.KeepDigits) ==
                    (balance.OptimalRangeEndValue - balance.TargetValue).KeepDigits(balance.KeepDigits))
                {
                    percentageSE = (balance.OptimalRangeEndValue - balance.TargetValue) / balance.TargetValue;
                }

                nearStartValue = balance.TargetValue - balance.TargetValue * percentageSE;
                minValue = balance.TargetValue - (balance.TargetValue * percentageSE * 3);
                nearEndValue = balance.TargetValue + balance.TargetValue - nearStartValue;
                maxValue = balance.TargetValue + balance.TargetValue - minValue;
            } 
            #endregion

            if (balance.CurrentValue.KeepDigits(balance.KeepDigits) < nearStartValue)
            {
                #region # 未达到接近值

                balance.Foreground = Balance.NormalColor;
                balance.AccordBrush = new SolidColorBrush(Colors.Transparent);
                balance.NearBrush = new SolidColorBrush(Colors.Transparent);
                balance.WarningBrush = new SolidColorBrush(Colors.Transparent);

                balance.NearVisibility = Visibility.Collapsed;
                balance.NormalVisibility = Visibility.Visible;
                minValue = balance.CurrentValue * (balance.Percentage / 100);
                maxValue = balance.TargetValue + (balance.TargetValue - minValue);
                balance.MiniValue = minValue;
                balance.MaxValue = maxValue;

                double rValue = (balance.TargetValue - minValue);
                balance.MinNearStartValueWidth = new GridLength((double)((nearStartValue - minValue) / rValue), GridUnitType.Star);
                balance.NearStartValueWidth = new GridLength((double)((balance.OptimalRangeStartValue - nearStartValue) / rValue), GridUnitType.Star);
                balance.StartValueWidth = new GridLength((double)((balance.TargetValue - balance.OptimalRangeStartValue) / rValue), GridUnitType.Star);

                balance.MaxNearEndValueWidth = new GridLength((double)((nearStartValue - minValue) / rValue), GridUnitType.Star);
                balance.NearEndValueWidth = new GridLength((double)((balance.OptimalRangeStartValue - nearStartValue) / rValue), GridUnitType.Star);
                balance.EndValueWidth = new GridLength((double)((balance.OptimalRangeEndValue - balance.TargetValue) / rValue), GridUnitType.Star);

                balance.Prompt = "即将称重："; 
                #endregion
            }
            else if (balance.CurrentValue.KeepDigits(balance.KeepDigits) >= nearStartValue&&balance.CurrentValue.KeepDigits(balance.KeepDigits) < balance.OptimalRangeStartValue.KeepDigits(balance.KeepDigits))
            {
                #region # 超过接近值小于目标起始值  即将达到目标值
                if (balance.CurrentValue >= balance.TargetValue - balance.TargetValue * 0.05 && balance.CurrentValue < balance.TargetValue - balance.TargetValue * 0.02)
                {
                    nearStartValue = balance.TargetValue - balance.TargetValue * 0.05;
                    nearEndValue = balance.TargetValue + balance.TargetValue - nearStartValue;
                    minValue = balance.TargetValue - (balance.TargetValue) * 0.15;
                    maxValue = balance.TargetValue + (balance.TargetValue - minValue);

                }
                else if (balance.CurrentValue >= balance.TargetValue - balance.TargetValue * 0.02 && balance.CurrentValue < balance.TargetValue - balance.TargetValue * 0.008)
                {
                    nearStartValue = balance.TargetValue - balance.TargetValue * 0.02;
                    nearEndValue = balance.TargetValue + balance.TargetValue - nearStartValue;
                    minValue = balance.TargetValue - balance.TargetValue * 0.06;
                    maxValue = balance.TargetValue + (balance.TargetValue - minValue);
                }
                else if (balance.CurrentValue >= balance.TargetValue - balance.TargetValue * 0.008 && balance.CurrentValue < balance.TargetValue - balance.TargetValue * 0.003)
                {
                    nearStartValue = balance.TargetValue - balance.TargetValue * 0.008;
                    nearEndValue = balance.TargetValue + balance.TargetValue - nearStartValue;
                    minValue = balance.TargetValue - balance.TargetValue * 0.016;
                    maxValue = balance.TargetValue + (balance.TargetValue - minValue);

                }
                else if (balance.CurrentValue >= balance.TargetValue - balance.TargetValue * 0.003 && balance.CurrentValue < balance.TargetValue - balance.TargetValue * 0.001)
                {
                    nearStartValue = balance.TargetValue - balance.TargetValue * 0.003;
                    nearEndValue = balance.TargetValue + balance.TargetValue - nearStartValue;
                    minValue = balance.TargetValue - balance.TargetValue * 0.009;
                    maxValue = balance.TargetValue + (balance.TargetValue - minValue);

                }
                else if (balance.CurrentValue >= balance.TargetValue - balance.TargetValue * 0.001)
                {
                    nearStartValue = balance.TargetValue - balance.TargetValue * 0.001;
                    nearEndValue = balance.TargetValue + balance.TargetValue - nearStartValue;
                    minValue = balance.TargetValue - balance.TargetValue * 0.003;
                    maxValue = balance.TargetValue + (balance.TargetValue - minValue);
                }

                balance.Foreground = Balance.NearColor;
                balance.AccordBrush = Balance.AccordColor;
                balance.NearBrush = Balance.NearColor;
                balance.WarningBrush = new SolidColorBrush(Colors.Transparent);

                balance.NearVisibility = Visibility.Visible;
                balance.NormalVisibility = Visibility.Collapsed;

                balance.MiniValue = minValue;
                balance.MaxValue = maxValue;

                balance.MinNearStartValueWidth = new GridLength((double)(nearStartValue - minValue), GridUnitType.Star);
                balance.NearStartValueWidth = new GridLength((double)(balance.OptimalRangeStartValue - nearStartValue), GridUnitType.Star);
                balance.StartValueWidth = new GridLength((double)(balance.TargetValue - balance.OptimalRangeStartValue), GridUnitType.Star);

                balance.MaxNearEndValueWidth = new GridLength((double)(balance.OptimalRangeEndValue - balance.TargetValue), GridUnitType.Star);
                balance.NearEndValueWidth = new GridLength((double)(nearEndValue - balance.OptimalRangeEndValue), GridUnitType.Star);
                balance.EndValueWidth = new GridLength((double)(maxValue - nearEndValue), GridUnitType.Star);


                balance.Prompt = "即将达到目标值："; 
                #endregion
            }
            else if (balance.CurrentValue.KeepDigits(balance.KeepDigits) >= balance.OptimalRangeStartValue.KeepDigits(balance.KeepDigits) && balance.CurrentValue.KeepDigits(balance.KeepDigits) <= balance.OptimalRangeEndValue.KeepDigits(balance.KeepDigits))
            {
                #region # 达到目标值

                balance.Foreground = Balance.AccordColor;
                balance.AccordBrush = Balance.AccordColor;
                balance.NearBrush = new SolidColorBrush(Colors.Transparent);
                balance.WarningBrush = new SolidColorBrush(Colors.Transparent);

                balance.NearVisibility = Visibility.Visible;
                balance.NormalVisibility = Visibility.Collapsed;

                double percentageSE = 0.001;

                if (balance.TargetValue - balance.OptimalRangeStartValue >
                    balance.OptimalRangeEndValue - balance.TargetValue)
                {
                    percentageSE = (balance.TargetValue - balance.OptimalRangeStartValue) / balance.TargetValue;
                }
                else if (balance.TargetValue - balance.OptimalRangeStartValue <
                    balance.OptimalRangeEndValue - balance.TargetValue)
                {
                    percentageSE = (balance.OptimalRangeEndValue - balance.TargetValue) / balance.TargetValue;
                }
                else if (balance.TargetValue - balance.OptimalRangeStartValue ==
                    balance.OptimalRangeEndValue - balance.TargetValue)
                {
                    if (balance.OptimalRangeEndValue - balance.TargetValue > 0)
                    {
                        percentageSE = (balance.OptimalRangeEndValue - balance.TargetValue) / balance.TargetValue;
                    }
                }

                nearStartValue = balance.TargetValue - balance.TargetValue * percentageSE;
                minValue = balance.TargetValue - (balance.TargetValue * percentageSE * 3);
                nearEndValue = balance.TargetValue + balance.TargetValue - nearStartValue;
                maxValue = balance.TargetValue + balance.TargetValue - minValue;

                balance.MiniValue = minValue;
                balance.MaxValue = maxValue;

                balance.MinNearStartValueWidth = new GridLength((double)(nearStartValue - minValue), GridUnitType.Star);
                balance.NearStartValueWidth = new GridLength((double)(balance.OptimalRangeStartValue - nearStartValue), GridUnitType.Star);
                balance.StartValueWidth = new GridLength((double)(balance.TargetValue - balance.OptimalRangeStartValue), GridUnitType.Star);

                balance.MaxNearEndValueWidth = new GridLength((double)(balance.OptimalRangeEndValue - balance.TargetValue), GridUnitType.Star);
                balance.NearEndValueWidth = new GridLength((double)(nearEndValue - balance.OptimalRangeEndValue), GridUnitType.Star);
                balance.EndValueWidth = new GridLength((double)(maxValue - nearEndValue), GridUnitType.Star);

                balance.Prompt = "达到目标值："; 
                #endregion
            }
            else if (balance.CurrentValue.KeepDigits(balance.KeepDigits) > balance.OptimalRangeEndValue.KeepDigits(balance.KeepDigits) && balance.CurrentValue.KeepDigits(balance.KeepDigits) <= nearEndValue)
            {
                #region # 超出目标值小于接近最大值
                //超出值
                balance.ExceedVisibility = Visibility.Visible;
                balance.Foreground = Balance.WarningColor;
                balance.AccordBrush = new SolidColorBrush(Colors.Transparent);
                balance.NearBrush = new SolidColorBrush(Colors.Transparent);
                balance.WarningBrush = Balance.WarningColor;

                balance.NearVisibility = Visibility.Visible;
                balance.NormalVisibility = Visibility.Collapsed;

                balance.MiniValue = balance.TargetValue - (balance.CurrentValue - balance.TargetValue) * 2;
                balance.MaxValue = balance.TargetValue + (balance.CurrentValue - balance.TargetValue) * 2;

                double rValue = (balance.TargetValue - minValue);
                balance.MinNearStartValueWidth = new GridLength((double)((nearStartValue - minValue) / rValue), GridUnitType.Star);
                balance.NearStartValueWidth = new GridLength((double)((balance.OptimalRangeStartValue - nearStartValue) / rValue), GridUnitType.Star);
                balance.StartValueWidth = new GridLength((double)((balance.TargetValue - balance.OptimalRangeStartValue) / rValue), GridUnitType.Star);

                balance.MaxNearEndValueWidth = new GridLength((double)((nearStartValue - minValue) / rValue), GridUnitType.Star);
                balance.NearEndValueWidth = new GridLength((double)((balance.OptimalRangeStartValue - nearStartValue) / rValue), GridUnitType.Star);
                balance.EndValueWidth = new GridLength((double)((balance.OptimalRangeEndValue - balance.TargetValue) / rValue), GridUnitType.Star);

                balance.Prompt = "超出目标值："; 
                #endregion
            }
            else if (balance.CurrentValue.KeepDigits(balance.KeepDigits) > nearEndValue)
            {
                #region # 超出目标值大于接近最大值
                //超出值
                balance.ExceedVisibility = Visibility.Visible;
                balance.Foreground = Balance.WarningColor;
                balance.AccordBrush = new SolidColorBrush(Colors.Transparent);
                balance.NearBrush = new SolidColorBrush(Colors.Transparent);
                balance.WarningBrush = Balance.WarningColor;

                balance.NearVisibility = Visibility.Visible;
                balance.NormalVisibility = Visibility.Collapsed;

                balance.MiniValue = balance.TargetValue - (balance.CurrentValue - balance.TargetValue) * 2;
                balance.MaxValue = balance.TargetValue + (balance.CurrentValue - balance.TargetValue) * 2;

                balance.MinNearStartValueWidth = new GridLength(20, GridUnitType.Star);
                balance.NearStartValueWidth = new GridLength(30, GridUnitType.Star);
                balance.StartValueWidth = new GridLength(50, GridUnitType.Star);

                balance.MaxNearEndValueWidth = new GridLength(20, GridUnitType.Star);
                balance.NearEndValueWidth = new GridLength(30, GridUnitType.Star);
                balance.EndValueWidth = new GridLength(50, GridUnitType.Star);

                balance.Prompt = "超出目标值："; 
                #endregion
            }
        }
        #endregion

        #region 剩余值—— double ResidualValue
        /// <summary>
        /// 剩余值
        /// </summary>
        public double ResidualValue
        {
            get { return (double)GetValue(ResidualValueProperty); }
            set { SetValue(ResidualValueProperty, value); }
        }
        /// <summary>
        /// 剩余值
        /// </summary>
        public static readonly DependencyProperty ResidualValueProperty = DependencyProperty.Register("ResidualValue", typeof(double), typeof(Balance), new PropertyMetadata(0.0, new PropertyChangedCallback(OnResidualValuePropertyChanged)));

        private static void OnResidualValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion

        #region 目标值—— double TargetValue
        /// <summary>
        /// 目标值
        /// </summary>
        public double TargetValue
        {
            get { return (double)GetValue(TargetValueProperty); }
            set { SetValue(TargetValueProperty, value); }
        }
        /// <summary>
        /// 目标值
        /// </summary>
        public static readonly DependencyProperty TargetValueProperty = DependencyProperty.Register("TargetValue", typeof(double), typeof(Balance), new PropertyMetadata(0.0, new PropertyChangedCallback(OnTargetValuePropertyChanged)));

        private static void OnTargetValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Balance balance = d as Balance;
            if (balance == null) return;
            double oldValue = 0;
            double.TryParse(e.OldValue.ToString(), out oldValue);
            //未达到接近值
            balance.Foreground = Balance.NormalColor;
            balance.AccordBrush = new SolidColorBrush(Colors.Transparent);
            balance.NearBrush = new SolidColorBrush(Colors.Transparent);
            balance.NearVisibility = Visibility.Collapsed;
            balance.NormalVisibility = Visibility.Visible;
            balance.MiniValue = 0.0;
            balance.MaxValue = balance.TargetValue;

            if (oldValue == 0 || balance.TargetValue == 0)
            {
                if (balance.TargetValue == 0)
                {
                    balance.ResidualValue = 0;
                }
                else
                {
                    balance.ResidualValue = balance.TargetValue - balance.CurrentValue;
                }
            }
            else
            {
                balance.ResidualValue = balance.TargetValue;
            }
            balance.Prompt = "即将称重：";
        }
        #endregion

        #region 最小值—— double MiniValue
        /// <summary>
        /// 最小值
        /// </summary>
        public double MiniValue
        {
            get { return (double)GetValue(MiniValueProperty); }
            set { SetValue(MiniValueProperty, value); }
        }
        /// <summary>
        /// 最小值
        /// </summary>
        public static readonly DependencyProperty MiniValueProperty = DependencyProperty.Register("MiniValue", typeof(double), typeof(Balance), new PropertyMetadata(0.0));

        #endregion

        #region 最大值—— double MaxValue
        /// <summary>
        /// 最大值
        /// </summary>
        public double MaxValue
        {
            get { return (double)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }
        /// <summary>
        /// 最大值
        /// </summary>
        public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(Balance), new PropertyMetadata(0.0));

        #endregion
        #region 单位——string UnitNo
        /// <summary>
        /// 单位
        /// </summary>
        public string UnitNo
        {
            get { return (string)GetValue(UnitNoProperty); }
            set { SetValue(UnitNoProperty, value); }
        }
        /// <summary>
        /// 单位
        /// </summary>
        public static readonly DependencyProperty UnitNoProperty = DependencyProperty.Register("UnitNo", typeof(string), typeof(Balance), new PropertyMetadata("KG", new PropertyChangedCallback(OnUnitNoPropertyChanged)));

        private static void OnUnitNoPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion


        #region 提示信息——string Prompt
        /// <summary>
        /// 提示信息
        /// </summary>
        public string Prompt
        {
            get { return (string)GetValue(PromptProperty); }
            set { SetValue(PromptProperty, value); }
        }
        /// <summary>
        /// 提示信息
        /// </summary>
        public static readonly DependencyProperty PromptProperty = DependencyProperty.Register("Prompt", typeof(string), typeof(Balance), new PropertyMetadata(""));

        #endregion

        #region 接近时的颜色——SolidColorBrush NearBrush
        /// <summary>
        /// 接近时的颜色
        /// </summary>
        public SolidColorBrush NearBrush
        {
            get { return (SolidColorBrush)GetValue(NearBrushProperty); }
            set { SetValue(NearBrushProperty, value); }
        }
        /// <summary>
        /// 接近时的颜色
        /// </summary>
        public static readonly DependencyProperty NearBrushProperty = DependencyProperty.Register("NearBrush", typeof(SolidColorBrush), typeof(Balance), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        #endregion

        #region 符合时的颜色——SolidColorBrush AccordBrush
        /// <summary>
        /// 符合时的颜色
        /// </summary>
        public SolidColorBrush AccordBrush
        {
            get { return (SolidColorBrush)GetValue(AccordBrushProperty); }
            set { SetValue(AccordBrushProperty, value); }
        }
        /// <summary>
        /// 符合时的颜色
        /// </summary>
        public static readonly DependencyProperty AccordBrushProperty = DependencyProperty.Register("AccordBrush", typeof(SolidColorBrush), typeof(Balance), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        #endregion

        #region 超出时的颜色——SolidColorBrush WarningBrush
        /// <summary>
        /// 超出时的颜色
        /// </summary>
        public SolidColorBrush WarningBrush
        {
            get { return (SolidColorBrush)GetValue(WarningBrushProperty); }
            set { SetValue(WarningBrushProperty, value); }
        }
        /// <summary>
        /// 超出时的颜色
        /// </summary>
        public static readonly DependencyProperty WarningBrushProperty = DependencyProperty.Register("WarningBrush", typeof(SolidColorBrush), typeof(Balance), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        #endregion


        #region 接近时的显示——Visibility NearVisibility
        /// <summary>
        /// 接近时的显示
        /// </summary>
        public Visibility NearVisibility
        {
            get { return (Visibility)GetValue(NearVisibilityProperty); }
            set { SetValue(NearVisibilityProperty, value); }
        }
        /// <summary>
        /// 接近时的颜色
        /// </summary>
        public static readonly DependencyProperty NearVisibilityProperty = DependencyProperty.Register("NearVisibility", typeof(Visibility), typeof(Balance), new PropertyMetadata(Visibility.Collapsed));
        #endregion

        #region 正常时的显示——Visibility NormalVisibility
        /// <summary>
        /// 正常时的显示
        /// </summary>
        public Visibility NormalVisibility
        {
            get { return (Visibility)GetValue(NormalVisibilityProperty); }
            set { SetValue(NormalVisibilityProperty, value); }
        }
        /// <summary>
        /// 正常时的显示
        /// </summary>
        public static readonly DependencyProperty NormalVisibilityProperty = DependencyProperty.Register("NormalVisibility", typeof(Visibility), typeof(Balance), new PropertyMetadata(Visibility.Visible));
        #endregion

        #region 超出时的显示——Visibility WarningVisibility
        /// <summary>
        /// 超出时的显示
        /// </summary>
        public Visibility WarningVisibility
        {
            get { return (Visibility)GetValue(WarningVisibilityProperty); }
            set { SetValue(WarningVisibilityProperty, value); }
        }
        /// <summary>
        /// 超出时的显示
        /// </summary>
        public static readonly DependencyProperty WarningVisibilityProperty = DependencyProperty.Register("WarningVisibility", typeof(Visibility), typeof(Balance), new PropertyMetadata(Visibility.Visible));
        #endregion
        #endregion
    }
}
